package nc.bs.pub.action;

import nc.bs.arap.bastradisc.plugin.bpplugin.BasTradiscPluginPoint;
import nc.bs.arap.bastradisc.rule.DataCheckRule;
import nc.bs.arap.bastradisc.rule.RateOrPriceCheckRule;
import nc.bs.arap.bastradisc.rule.UniqueCheckRule;
import nc.bs.framework.common.NCLocator;
import nc.bs.pubapp.pf.action.AbstractPfAction;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.vo.arap.bastradisc.AggBasDiscVO;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class N_DCH1_SAVEBASE extends AbstractPfAction<AggBasDiscVO> {

	@Override
	protected CompareAroundProcesser<AggBasDiscVO> getCompareAroundProcesserWithRules(Object userObj) {
		CompareAroundProcesser<AggBasDiscVO> processor = null;
		AggBasDiscVO[] clientFullVOs = (AggBasDiscVO[]) this.getVos();
		/*
		 * BillTransferTool<AggBasDiscVO> tool = new BillTransferTool<AggBasDiscVO>(
		 * clientFullVOs); clientFullVOs = tool.getClientFullInfoBill();
		 */
		if (!StringUtil.isEmptyWithTrim(clientFullVOs[0].getParentVO().getPrimaryKey())) {
			processor = new CompareAroundProcesser<AggBasDiscVO>(BasTradiscPluginPoint.SCRIPT_UPDATE);
		} else {
			processor = new CompareAroundProcesser<AggBasDiscVO>(BasTradiscPluginPoint.SCRIPT_INSERT);
		}
		
		IRule<AggBasDiscVO> rule = new UniqueCheckRule();
		processor.addAfterRule(rule);
		
		rule = new DataCheckRule();
		processor.addBeforeRule(rule);
		
		rule = new RateOrPriceCheckRule();
		processor.addBeforeRule(rule);
		
		
		return processor;
	}

	@Override
	protected AggBasDiscVO[] processBP(Object userObj, AggBasDiscVO[] clientFullVOs, AggBasDiscVO[] originBills) {

		AggBasDiscVO[] bills = null;
		try {
			nc.itf.arap.IBasTradiscMaintain operator = NCLocator.getInstance()
					.lookup(nc.itf.arap.IBasTradiscMaintain.class);
			if (!StringUtil.isEmptyWithTrim(clientFullVOs[0].getParentVO().getPrimaryKey())) {
				bills = operator.update(clientFullVOs, originBills);
			} else {
				bills = operator.insert(clientFullVOs, originBills);
			}
		} catch (BusinessException e) {
			ExceptionUtils.wrappBusinessException(e.getMessage());
		}
		return bills;
	}
}
